#include <bits/stdc++.h>
using namespace std;

const int N(1e5 + 5);
int n, m, noa[N], c[N], a[N];
bool d[20][N], vis[N];

int dfs(int l, int r){
    if(l == r) return l;
    int mid = (l + r) >> 1;
    int l1 = dfs(l, mid), r1 = dfs(mid + 1, r);
    int R = log2(r - l + 1), G = r >> R;
    int k;
    if(d[R][G]) k = l1;
    else k = r1;
    if(a[k] >= R) return k;
    if(k == l1) return r1;
    return l1;
}

int main(){
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    cin >> n >> m;
    for(int i = 1; i <= n; i++) cin >> noa[i];
    for(int i = 1; i <= m; i++) cin >> c[i];
    int k = ceil(log2(n));
    for(int i = 1; i <= k; i++){
        for(int j = 1; j <= (1 << (k - i)); j++){
            char e;
            cin >> e;
            d[i][j] = e - '0';
        }
    }
    int T;
    cin >> T;
    while(T--){
        int x0, x1, x2, x3;
        cin >> x0 >> x1 >> x2 >> x3;
        memcpy(a, noa, sizeof a);
        for(int i = 1; i <= n; i++){
            if(i % 4 == 1) a[i] ^= x1;
            else if(i % 4 == 2) a[i] ^= x2;
            else if(i % 4 == 3) a[i] ^= x3;
            else a[i] ^= x0;
        }
        long long sum(0);
        for(int i = 1; i <= m; i++){
            sum ^= 1LL * dfs(1, 1 << k);
        }
        cout << sum << '\n';
    }
    return 0;
}
